import uvicorn
from uvicorn.middleware.wsgi import WSGIMiddleware
from server import app, user_opt, db
from server.api import init_api_route
from server.app import init_app_route

# api路由注册
from server.model.sys.sys_log import SysLog

init_api_route(app)
# app路由注册
init_app_route(app)


@user_opt.connect
def on_user_opt(args):
    if "查询" in args["operation"]:
        return
    opt = SysLog(user_id=args['user_id'], username=args["username"], operation=args["operation"], method=args["method"], params=args['params'])
    db.session.add(opt)
    db.session.commit()


if __name__ == "__main__":
    # 使用WSGI中间件包装Flask应用
    uvicorn.run(
        WSGIMiddleware(app),
        host="0.0.0.0",
        port=app.config['PORT'],
        log_level="info"
    )